﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.MODEL.SmallPic;
using MINERP.DAL;

namespace MINERP.BLL.OldSmallPic
{
    public class OldPrintConfigBLL
    {
        /// <summary>
        /// 获得打印机设置
        /// </summary>
        /// <param name="printerId"></param>
        /// <returns></returns>
        public PrinterConfigInfo GetPrinterConfigInfo(int printerId)
        {
            PrinterConfigInfo pcInfo = new PrinterConfigInfo();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                PrinterConfig pc = enty.PrinterConfig.FirstOrDefault(p => p.printer.PrinterID == printerId);
                if (pc != null)
                {
                    if (!pc.printerReference.IsLoaded)
                    {
                        pc.printerReference.Load();
                    }

                    pcInfo.IsAutoPrintTikect = pc.IsAutoPrintTikect;
                    pcInfo.PrinterConfigID = pc.PrinterConfigID;
                    pcInfo.PrinterID = pc.printer.PrinterID;
                    pcInfo.PrinterGridNum = pc.PrinterGridNum;
                }
                else
                {
                    pcInfo = null;
                }
            }
            return pcInfo;
        }

        /// <summary>
        /// 保存打印机设置
        /// </summary>
        /// <param name="printerId"></param>
        /// <param name="isAutoPrintTicket"></param>
        /// <param name="printerGridNum"></param>
        /// <returns></returns>
        public int UpdatePrinterConfig(int printerId,bool isAutoPrintTicket,int printerGridNum)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
                {
                    PrinterConfig pc = enty.PrinterConfig.FirstOrDefault(p => p.printer.PrinterID == printerId);
                    if (pc == null)
                    {
                        PrinterConfig pconfig = new PrinterConfig();
                        pconfig.IsAutoPrintTikect = isAutoPrintTicket;
                        pconfig.printer = enty.printer.First(p => p.PrinterID == printerId);
                        pconfig.PrinterGridNum = printerGridNum;
                        enty.AddToPrinterConfig(pconfig);
                    }
                    else
                    {
                        pc.PrinterGridNum = printerGridNum;
                        pc.IsAutoPrintTikect = isAutoPrintTicket;

                    }
                    enty.SaveChanges();
                }
            }
            catch
            {
                ret = 1;
            }
            return ret;
        }
    }
}
